package com.ruoyi.paipan.common;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SolarHalfYear {
    private int year;
    private int month;
    public static int MONTH_COUNT = 6;

    public SolarHalfYear(int year, int month) {
        this.year = year;
        this.month = month;
    }

    @Override
    public String toString()
    {
        return this.year + "." + this.getIndex();
    }

    public String toFullString()
    {
        return this.year + "年" + (1 == this.getIndex() ? "上" : "下") + "半年";
    }

    public static SolarHalfYear fromYm(int year, int month)
    {
        return new SolarHalfYear(year, month);
    }

    public static SolarHalfYear fromDate(Date date)
    {
        Solar solar = Solar.fromDate(date);
        return new SolarHalfYear(solar.getYear(), solar.getMonth());
    }

    public int getYear()
    {
        return this.year;
    }

    public int getMonth()
    {
        return this.month;
    }

    public int getIndex()
    {
        return (int)Math.ceil((double) this.month / SolarHalfYear.MONTH_COUNT);
    }

    public List<SolarMonth> getMonths()
    {
        List<SolarMonth> l = new ArrayList<>();
        int index = this.getIndex() - 1;
        for (int i = 0; i < SolarHalfYear.MONTH_COUNT; i++) {
            l.add(new SolarMonth(this.year, SolarHalfYear.MONTH_COUNT * index + i + 1));
        }
        return l;
    }

    public SolarHalfYear next(int halfYears)
    {
        SolarMonth month = SolarMonth.fromYm(this.year, this.month).next(MONTH_COUNT * halfYears);
        return new SolarHalfYear(month.getYear(), month.getMonth());
    }

    public static void main(String[] args) {
        System.out.println(SolarHalfYear.fromYm(2020, 7));
    }
}
